﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using System.Data.Linq;
using QLTVNEW.Report;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

namespace QLTVNEW
{
    public partial class frmInDG :DevComponents.DotNetBar.Office2007Form
    {
        public frmInDG()
        {
            InitializeComponent();
            DataLoadOptions dlo = new DataLoadOptions();
            dlo.LoadWith<DOCGIA>(dg => dg.LOAIDOCGIA);
            db.LoadOptions = dlo;
        }

        QLTVDataContext db = new QLTVDataContext();
        //SACH tbs = new SACH();
        DOCGIA tbdg = new DOCGIA();
        //NHAXUATBAN tbnxb = new NHAXUATBAN();
        //TACGIA tbtg = new TACGIA();
        //TINHTRANG tbtr = new TINHTRANG();
        //THELOAI tbtl = new THELOAI();
        rptDG rpt = new rptDG();

        InDGMuonSach rpt2 = new InDGMuonSach();
           
        void GetTree()
        {
            treeView1.Nodes.Clear();
            var query = from tbdgs in db.DOCGIAs
                        select new
                        {
                            tbdgs.HoTen
                        };
          
         //  tparent.Text = "Tên Độc Giả";
            TreeNode node;
            node = treeView1.Nodes.Add("Tên Độc Giả");
            foreach (var item in query)
            {   
                
                node.Nodes.Add(item.HoTen);
                node.Expand();  
            }

            
        }


        private void frmInDG_Load(object sender, EventArgs e)
        {
            
            //var query = (from tb in db.DOCGIAs
            //             select new 
            //             {
            //                 tb.MaDocGia,
            //                 tb.HoTen,
            //                 tb.NgaySinh,
            //                 tb.DiaChi,
            //                 tb.Email,
            //                 tb.NgayLapThe,
            //                 tb.NgayHetHan,
            //                 tb.TinhTrangThe
            //             }).ToList();


            //GetTree();

            ////ConnectionInfo conn = new ConnectionInfo();
            ////TableLogOnInfo tablelog = new TableLogOnInfo();
            ////conn.ServerName = @".\sqlexpress1";
            ////conn.IntegratedSecurity = true;
            ////conn.DatabaseName = "QuanLyTV";

            ////tablelog.ConnectionInfo = conn;

            ////foreach (Table tablename in rpt.Database.Tables)
            ////{
            ////    tablename.ApplyLogOnInfo(tablelog);
            ////}
            //// Hiển thị dữ liệu
       
            //rpt.SetDataSource(query);
            //this.crystalReportViewer1.ReportSource = rpt;
         
        }

     

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Action==TreeViewAction.ByMouse)
            {
                int ma = tbdg.GetMaDocGia(e.Node.Text);
                
                var query=(from tbp in db.PHIEUMUONTRASACHes
                            join tbdgs in db.DOCGIAs on tbp.MaDocGia equals tbdgs.MaDocGia
                            join tbsa in db.SACHes on tbp.MaSach equals tbsa.MaSach
                            join tbtls in db.THELOAIs on tbsa.MaTheLoai equals tbtls.MaTheLoai
                            join tbnxbs in db.NHAXUATBANs on tbsa.NhaXuatBan equals tbnxbs.MaNXB
                            join tbtgs in db.TACGIAs on tbsa.TacGia equals tbtgs.MaTacGia
                            where tbp.MaDocGia==ma
                               select new 
                        {
                            tbsa.MaSach,
                            tbsa.TenSach,
                            tbtls.TenTheLoai,
                            tbnxbs.TenNXB,
                            tbtgs.TenTacGia,
                            tbsa.NamXuatBan
                        
                        }).ToList();
                 rpt2.SetDataSource(query);
                 rpt2.SetParameterValue("maDG", ma.ToString());
                 rpt2.SetParameterValue("tenDG", e.Node.Text.ToString());
                 this.crystalReportViewer1.ReportSource = rpt2;
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        private void máyInToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PrintDialog f = new PrintDialog();
            if (f.ShowDialog() == DialogResult.OK)
            {
                rpt.PrintToPrinter(1, true, -1, -1);
            }
        }

        private void fileWordToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.Title = "Save As";
            save.Filter = ".doc|*.doc";

            if (save.ShowDialog() == DialogResult.OK)
            {
                rpt.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.WordForWindows, save.FileName);
            }
        }

        private void fileEecelToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.Title = "Save As";
            save.Filter = ".xlsx|*.xlsx";

            if (save.ShowDialog() == DialogResult.OK)
            {
                rpt.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.Excel, save.FileName);
            }
        }

        private void filePDFToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.Title = "Save As";
            save.Filter = ".pdf|*.pdf";

            if (save.ShowDialog() == DialogResult.OK)
            {
                rpt.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, save.FileName);
            }
        }

        private void comboBoxEx1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxEx1.Text == "Danh Sách Độc Giả")
            {
                var query = (from tb in db.DOCGIAs
                             select new
                             {
                                 tb.MaDocGia,
                                 tb.HoTen,
                                 tb.NgaySinh,
                                 tb.DiaChi,
                                 tb.Email,
                                 tb.NgayLapThe,
                                 tb.NgayHetHan,
                                 tb.TinhTrangThe
                             }).ToList();

                //ConnectionInfo conn = new ConnectionInfo();
                //TableLogOnInfo tablelog = new TableLogOnInfo();
                //conn.ServerName = @".\sqlexpress1";
                //conn.IntegratedSecurity = true;
                //conn.DatabaseName = "QuanLyTV";

                //tablelog.ConnectionInfo = conn;

                //foreach (Table tablename in rpt.Database.Tables)
                //{
                //    tablename.ApplyLogOnInfo(tablelog);
                //}
                // Hiển thị dữ liệu

                rpt.SetDataSource(query);
                this.crystalReportViewer1.ReportSource = rpt;
                
            }
            if (comboBoxEx1.Text == "Độc Giả Mượn Sách")
            {

                GetTree();

            }
        }
    }
}
